<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
 * Description: Login controller class
 */
class Login extends CI_Controller{
    
    function __construct(){
        parent::__construct();
    }

    public function index(){
        // Load our view to be displayed
        // to the user
        $this->load->view('login');
    }

    public function check() {

        // Validate post data
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_login_data');
        $this->form_validation->set_error_delimiters('', '');
        if ($this->form_validation->run() == false) {
            $this->load->view('login');
        } else
            redirect('dashboard');
    }

    public function check_login_data($password) {
        // Load the model
        $this->load->model('login_model');
        $result = $this->login_model->validate();
        if($result)
            return true;
        else {
            $this->form_validation->set_message('check_login_data', 'Username or Password incorrect!');
            return false;
        }
    }

    public function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }
}
?>